import matplotlib.pyplot as plt
from matplotlib.figure import Figure

def attendance_fig(attendance_data) -> Figure:
    if len(attendance_data) == 0:
        fig = plt.figure(figsize=(6, 2))
        plt.gca().axis('off')
        plt.figtext(0.5, 0.5,
                   '无考勤数据', 
                   ha='center', 
                   va='center', 
                   fontsize=12,
                   color='gray',
                   alpha=0.8)
    else:
        fig = plt.figure(figsize=(6, 4))
        grouped = attendance_data.groupby('ControllerID').agg(
            count=('ControllerID', 'size'),
            controler_name=('controler_name', 'first')
            ).reset_index()
        patches, texts, autotexts = plt.pie(
            grouped['count'],
            labels=None,autopct='%1.1f%%',
            startangle=90,
            pctdistance=0.8,
            wedgeprops={'edgecolor': 'white', 'linewidth': 1}
        )
        plt.legend(
            patches,
            grouped['controler_name'],
            title='考勤',
            loc='center left',
            bbox_to_anchor=(1, 0.5),
            fontsize=10
        )
        plt.title('考勤数据分布\n', fontsize=14, fontweight='bold')
        plt.subplots_adjust(left=0.1, right=0.75)
        for autotext in autotexts:
            autotext.set_size(10)
            autotext.set_color('white')
            autotext.set_weight('bold')
    return fig